<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<!-- head 头部-->
<head th:replace="admin/layout/include::head('权限分配','/assets/libs/zTree/css/zTreeStyle/zTreeStyle.css')"></head>
<style>
    .layui-tab-content>.layui-tab-item{
        height: 300px;
        overflow-y: auto;
        overflow-x: hidden;
    }
    .layui-tab-content>.layui-tab-item>ul{
        padding: 5px;
    }
</style>
<body>

<div style="height: 350px;overflow-y: hidden;overflow-x: hidden;">
    <input id="roleId" type="hidden" th:value="${roleId}">
    <div class="layui-tab  layui-tab-brief">
        <ul class="layui-tab-title">
            <li class="layui-this">菜单权限</li>
            <li>帖子权限</li>
            <li>下载权限</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <ul id="menuPermissionTree" class="ztree"></ul>
            </div>
            <div class="layui-tab-item">
                <ul id="catalogPermissionTree" class="ztree"></ul>
            </div>
            <div class="layui-tab-item">
                <ul id="downloadAuthTree" class="ztree"></ul>
            </div>
        </div>
    </div>

</div>

<div class="text-right" style="padding-top: 10px;padding-right: 25px;">
    <button class="layui-btn layui-btn-primary" ew-event="closeDialog">取消</button>
    <button id="btnSave" class="layui-btn">保存</button>
</div>

<!-- js部分 -->
<div th:replace="admin/layout/include::js"></div>
<!--<script type="text/javascript" src="/assets/libs/zTree/js/jquery.ztree.all-3.5.min.js"></script>-->
<script>
    layui.use(['layer', 'admin','zTree','element'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var admin = layui.admin;
        var element = layui.element;
        var roleId = $('#roleId').val();
        layer.load(2);
        var setting = {
            check: {enable: true},
            data: {
                simpleData: {
                    enable: true,
                    idKey:"id",
                    pIdKey:"parentId",
                    rootPId:"1"
                }
            }
        };
        B.get({
            url:'/admin/role/permissionTree',
            data:{roleId,roleId},
            success:function(res){
                $.fn.zTree.init($('#menuPermissionTree'), setting, res.data.menuPermissionTree);
                $.fn.zTree.init($('#catalogPermissionTree'), setting, res.data.catalogPermissionTree);
                layer.closeAll('loading');
            }
        });




        // 保存按钮点击事件
        $('#btnSave').click(function () {
            layer.load(2);
            var permissions = new Array();
            var menuTreeObj = $.fn.zTree.getZTreeObj('menuPermissionTree');
            var menuNodes = menuTreeObj.getCheckedNodes(true);
            var menuIds = new Array();
            for (var i = 0; i < menuNodes.length; i++) {
                menuIds[i] = menuNodes[i].id;
            }
            permissions.push({'type':1,'permissionIds':menuIds});
            var catalogTreeObj = $.fn.zTree.getZTreeObj('catalogPermissionTree');
            var catalogNodes = catalogTreeObj.getCheckedNodes(true);
            var catalogIds = new Array();
            for (var i = 0; i < catalogNodes.length; i++) {
                catalogIds[i] = catalogNodes[i].id;
            }

            permissions.push({'type':2,'permissionIds':catalogIds});

            console.log(JSON.stringify(permissions));
            $.post('/admin/role/updateRolePermission', {
                roleId: roleId,
                permissions: JSON.stringify(permissions)
            }, function (data) {
                layer.closeAll('loading');
                if (data.success) {
                    top.layer.msg(data.msg, {icon: 1});
                    // 关闭当前iframe弹出层
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                } else {
                    top.layer.msg(data.msg, {icon: 2});
                }
            }, 'json');
        });
        //数组转换为json
        function arrayConvertJson(array){
            var jsonString = '[';
            for(i in array){
                if(!array[i].type)
                    continue;
                jsonString += "{\"type\":\""+array[i].type+"\",\"ids\":\""+array[i].ids+"\"}";
                if(i!=(array.length-1)){
                    jsonString += ',';
                }
            }
            jsonString += ']';
            return jsonString;
        }

    });
</script>

</body>
</html>